$(function(){
 		validate();
 	});
 	
 	var validate = function(){
		$("form").validate({
			rules:{
				uname:{
					required:true,
					rangelength:[2,20]
				},
				upassword:{
					required:true,
					rangelength:[6,20]
				}
			},
			messages:{
				uname:{
					required:"用户名未填写!",
					rangelength:$.validator.format("用户名长度为  2~20  个字符之间")
				},
				upassword:{
					required:"密码未填写!",
					rangelength:$.validator.format("用户密码长度为  6~20  个字符!")
				},
				ucaptcha:{
					required:"验证码未填写!",
					rangelength:$.validator.format("验证码错误!")
				}
			},
			onkeyup:false,
			 onfocusout:false,
			onclick : false, 
			errorContainer: "#msg",
			errorLabelContainer: $("#msg"),
			wrapper: "li",
			errorClass: "help-inline",
			errorElement: "span",
			showErrors:function(errorMap,errorList) {
		        $("#msg").empty();
		       
		        //$("#msg").hide();
				this.defaultShowErrors();
			},
			submitHandler : function(form) {  
					var requestValue=formValue(form);
		 			//提交表单
		 			formAjax(requestValue);
		 			return false;
			 }  
		});	
	}
 	
 	var formValue=function(form){
		var url= [$(form).attr("action"),".jhtml"].join("");
		var method= $(form).attr("method");
		var data = $( form ).serialize() ;
		var info={
			url:url,
			method:method,
			data:data
		}
		return info;
	}
 	
	var formAjax=function(info){
		$.ajax({
				type :info.method,
				url: info.url,
				data:info.data
	 		}).done(function(data){
	 			responseHandel(data);
		}).fail(function(data){
			//msg('操作失败');
		});
	}
	
	var responseHandel=function(data){
		if(data.success){
			location.href = data.data;
		}else{
			reloadCode();
		}
		msgshow(data.msg);
		
	}
	

  	//获取url中的参数
    function getUrlParam(name) {
    	//window.location.href=decodeURI(window.location.href); 
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
        var r = window.location.search.substr(1).match(reg);  //匹配目标参数
        if (r != null) return decodeURI(r[2]); return null; //返回参数值
    }
	

	
	
	
	
	
	//扫码次数倒计时
	var maxtime = 60;
	//清除setInterval
	var timer;
	//扫码请求
	function scancodeLogin(){
		--maxtime;
		if (maxtime == 0) {	
				$(".msgsaoma").html("<font color='red'>扫码已失效,如需扫码登录刷新页面</font>");
				clearInterval(timer);
			
		} else {
			//请求是否登录
			$.ajax({
				type : "POST",
				url : $('#path').val() + "/scancode",
				timeout : 120000, //设置2分钟
				data : {
					"uuid" : "${uuid}",
					"qrcodepath" :"${qrcodepath}",
					"num" :maxtime 
				},
				async : false,
				success : function(data) {
					
					if (data != ""&&data>0) {
						$("#aid").val(data);						
						clearInterval(timer);						
						logSub.submit();
					}
				},
				error : function() {
					clearInterval(timer);
				}
			});
		}
	}
	
	$(document).keypress(function(e) {  
	      if(e.which == 13) {  
	    	  $("#loginBt").click();
	       }  
	   }); 
	
	
	function e(e) {
		$("#err").html("<span class='err_tips'><strong>" + e + "</strong></span>").show();
	}
	
	function reCheckcode(img) {
		img.src = "drawCheckCode?" + Math.random();
	}

	$(document).ready(function() {
				$("#loginBt").on("click",
						function() {
							if ($.trim($("#account").val()).length == 0) {
								e("你还没有输入帐号！"), $("#account").focus();
								return;
							}
							if ($.trim($("#pwd").val()).length == 0) {
								e("你还没有输入密码！"), $("#pwd").focus();
								return;
							}

							$.ajax({
								type : "POST",
								url : "login",
								data : {"account":$.trim($("#account").val()),
										"password": $.trim($("#pwd").val()),
										"checkcode": $.trim($("#checkcode").val()),	
										"qrcodepath": $("#qrcodepath").val(),
										"rememberMe":0
									},
								dataType : "json",
								success : function(data) {
									if (data.message>0) {	
										$("#aid").val(data.message);	
										logSub.submit();
										//location.href = "home";
									} else if(data.message==0) {
										e("账号或密码错误！");
										if(data.showCheckCode==1){
											$("#showCheckCodeDiv").css("display","block");
											$("#checkcodeImg").attr("src","drawCheckCode?" + Math.random());
										}
									}else {
										e("验证码输入错误！");
										if(data.showCheckCode==1){
											$("#showCheckCodeDiv").css("display","block");
											$("#checkcodeImg").attr("src","drawCheckCode?" + Math.random());
										}
									}
								}
							});
						});
			
				//验证码显示
				if($("#showCheckCode").val()==1){
					$("#showCheckCodeDiv").css("display","block");
				}	
				
				//每两秒调用扫码
				timer= window.setInterval(function () {              
                 scancodeLogin();}, 2000);                          			       
			});
		